package com.yt.system.dao;

import com.yt.system.domain.*;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.LinkedList;

@Repository
public interface QuestionsMapper {
    int deleteByPrimaryKey(Integer qid);

    int insert(Questions record);

    int insertSelective(Questions record);

    Questions selectByPrimaryKey(Integer qid);

    int updateByPrimaryKeySelective(Questions record);

    int updateByPrimaryKey(Questions record);

    /**
     * 选出题目给组员相互评价（题目数量是10道题）
     * @return
     */
    @Select("select * from questions order by rand() limit 10")
    ArrayList<Questions> getQuestion();

    /**
     * 按照qid查找题目的选项分值
     * @param qid
     * @return
     */
    @Select("select * from questioncontent_value where qid = #{qid}")
    QuestionContentValue findQuestionContentValueByQid(Integer qid);

    /**
     * 将题目存进数据库
     * @param questions
     */
    @Options(useGeneratedKeys = true,keyProperty = "qid")
    @Insert("insert into questions values (null,#{question},#{contenta},#{contentb},#{contentc},#{contentd},#{contente},#{createtime})")
    int insertQuestions(Questions questions);

    /**
     * 将题目的选项值的分数值存进数据库
     * @param questionContentValue
     */
    @Insert("insert into questioncontent_value values (null,#{qid},#{avalue},#{bvalue},#{cvalue},#{dvalue},#{evalue})")
    void insertQuestionContentValue(QuestionContentValue questionContentValue);

    /**
     *
     * @description 根据用户uid查询用户的评分对象
     * @author 高铭
     * @Date   2019/9/30 - 9:30
     * @Param  uid
     * @return com.yt.system.domain.Sorttable
     *
     */
    @Select("select * from sorttable where uid = #{uid}")
    Sorttable findByUid(Integer uid);

    /**
     * @description 添加用户的评分记录
     * @author 高铭
     * @Date   2019/9/30 - 9:37
     * @Param  [sorttable]
     * @return void
     */
    @Insert("insert into sorttable values (null,#{uid},#{gid},#{score},#{sorttime})")
    void insertGroupPeopleScore(Sorttable sorttable);

    /**
     * @description  修改用户的评分成绩
     * @author 高铭
     * @Date   2019/9/30 - 9:41
     * @Param  [uid, score]
     * @return void
     */
    @Update("update sorttable set score = score + #{score} where uid = #{uid}")
    void updateGroupPeopleScore(@Param("uid") Integer uid, @Param("score") Integer score);

    /**
     * @description 传入用户的组别gid得到排名好的用户信息
     * @author 高铭
     * @Date   2019/9/30 - 10:20
     * @Param  [gid]
     * @return java.util.LinkedList<com.yt.system.domain.SortedUser>
     */
    @Select("select user.uname,st.uid,st.score from (select uid,score from sorttable where gid = #{gid} and sorttime" +
            " like '${sortTime}%') as st left join user on user.uid = st.uid order by score desc;")
    LinkedList<SortedUser> getTheGroupSortedPeople(@Param("gid") Integer gid,@Param("sortTime")String sortTime);

    /**
     * @description 根据组id拿到这个组的最后一名用户的uid
     * @author 高铭
     * @Date   2019/9/30 - 11:27
     * @Param  [gid]
     * @return java.lang.Integer
     */
    @Select("select uid from sorttable where gid = #{gid} order by min(score)")
    Integer getTheLastSortedUserOfId(Integer gid);

    /**
     * @description 查询最后一名的用户是否是组长
     * @author 高铭
     * @Date   2019/9/30 - 11:31
     * @Param  [theLastSortedUserOfId, gid]
     * @return com.yt.system.domain.UserRole
     */
    @Select("select * from user_role where aid = #{theLastSortedUserOfId} and roid = 2")
    UserRole findUserRole(@Param("theLastSortedUserOfId") Integer theLastSortedUserOfId,@Param("gid") Integer gid);

    /**
     * @description 最后一名是组长，则组长进行减分 总分 / 2
     * @author 高铭
     * @Date   2019/9/30 - 12:13
     * @Param  [theLastSortedUserOfId]
     * @return void
     */
    @Update("update sorttable set score = score / 2 where uid = #{theLastSortedUserOfId}")
    void updateGroupLeaderLowScore(Integer theLastSortedUserOfId);

    /**
     * @description 最后一名的人员分数 / 2
     * @author 高铭
     * @Date   2019/9/30 - 12:13
     * @Param  [theLastSortedUserOfId]
     * @return void
     */
    @Update("update sorttable set score = score / 2 where uid = #{theLastSortedUserOfId}")
    void updateGroupLastUserLowScore(Integer theLastSortedUserOfId);

    /**
     * @description 根据gid查询这个组的组长
     * @author 高铭
     * @Date   2019/9/30 - 12:35
     * @Param  [gid]
     * @return com.yt.system.domain.UserRole
     */
    @Select("select * from user_role where aid in (select uid from group_message where gid = #{gid} and status = 0) and roid = 2")
    UserRole getThgroupLeader(Integer gid);

    /**
     * @description 最后一名不是组长，给组长总分 * 2
     * @author 高铭
     * @Date   2019/9/30 - 12:39
     * @Param  [aid]
     * @return void
     */
    @Update("update sorttable set score = score * 2 where uid = #{aid}")
    void updateGroupLeaderUpScore(Integer aid);
}